$(function(){
	var baseUrl = "/baichengwandianqingxiaowang/";
	//添加认证信息
	$(".go_renzheng").on('click',function(){
		$(".authentication_info").before($("<div class='shadow'></div>"));
		$(".authentication_info").fadeIn();
		clickShadow(function(){
			$(".authentication_info").fadeOut();
		})
	});
	//点击选择地址
	$(".address_info").on("click",function(){
		
		$(".select_address").before($("<div class='shadow'></div>"))
		$(".select_address").css({
			opacity:"1",
			visibility:"visible"
		});
		clickShadow(function(){
			$(".select_address").css({
				opacity:"0",
				visibility:"hidden"
			})
		})
	});
	
//点击增加地址
$(".select_header").on("click",".floatRight",function(){
	$(".select_address").css({
			opacity:"0",
			visibility:"hidden"
	})
	$(".add_address").css({
			opacity:"1",
			visibility:"visible"
	})
	clickShadow(function(){
		$(".add_address").css({
			opacity:"0",
			visibility:"hidden"
		})
	})
})
//点击返回
$(".address_return").on("click",function(){
	$(".add_address").css({
			opacity:"0",
			visibility:"hidden"
	})
	$(".select_address").css({
			opacity:"1",
			visibility:"visible"
	})
});
//省级地址下拉框发生改变
$("#province").change(function(){
	var provinceId = $(this).val();
	var provinceName = $(this).find("option[value="+provinceId+"]").text();
	if(provinceName=='省份（必填）'){
		$("#city").empty();
		$("#city").prepend("<option value='city'>市（选填）</option>");
		$("#county").empty();
		$("#county").prepend("<option value='county'>县（选填）</option>");
	}else{
		$.ajax({
			type:"get",
			url:baseUrl+"customer/getAddressByParentId",
			data:{"parentId":provinceId},
			async:false,
			success:function(data){
				$("#city").empty();
				$("#city").prepend("<option value='city'>市（选填）</option>");
				$("#county").empty();
				$("#county").prepend("<option value='county'>县（选填）</option>");
				for(var i=0;i<data.length;i++){
					$("#city").append('<option value='+data[i].addressId+'>'+data[i].addressName+'</option>');
				}
			},
			error:function(){
			}
		});
	}
});
//市级下拉框发生改变
$("#city").change(function(){
	var cityId = $(this).val();
	var cityName = $(this).find("option[value="+cityId+"]").text();
	if(cityName=='市（选填）'){
		$("#county").empty();
		$("#county").prepend("<option value='county'>县（选填）</option>");
	}else{
		$.ajax({
			type:"get",
			url:baseUrl+"customer/getAddressByParentId",
			data:{"parentId":cityId},
			async:false,
			success:function(data){
				$("#county").empty();
				$("#county").prepend("<option value='county'>县（选填）</option>");
				for(var i=0;i<data.length;i++){
					$("#county").append('<option value='+data[i].addressId+'>'+data[i].addressName+'</option>');
				}
			},
			error:function(){
			}
		});
	}
});
//增加新的收货地址
$("#saveNewAddress").on("click",function(){
	var name = $("#name").val();
	var phone = $("#phone").val();
	var detailAddress = $("#detailAddress").val();
	var provinceId = $("#province").val();
	var provinceName = $("#province").find("option[value="+provinceId+"]").text();
	var isDefault;
	if(name!=""&&phone!=""&&detailAddress!=""&&provinceId!="province"){
		var cityId = $("#city").val();
		var cityName =$("#city").find("option[value="+cityId+"]").text();
		var newCityName;
		if(cityId=="city"){
			cityId = 0;
			cityName = null
			newCityName = "";
		}else{
			newCityName = cityName;
		}
		var countyId = $("#county").val();
		var countyName =$("#county").find("option[value="+countyId+"]").text();
		var newCountyName;
		if(countyId=="county"){
			countyId = 0;
			countyName = null;
			newCountyName = "";
		}else{
			newCountyName = countyName;
		}
		if($(".select").hasClass("iconfont")){
			isDefault = "1";
		}else{
			isDefault = "0";
		}
		var json = {
				"oneAddressId" : provinceId,
				"oneAddressName" : provinceName,
				"twoAddressId" : cityId,
				"twoAddressName" : cityName,
				"threeAddressId" : countyId,
				"threeAddressName" : countyName,
				"detailAddress" : detailAddress,
				"contactsPerson" : name,
				"contactsWay" : phone,
				"isDefaultReceiptAddress" : isDefault
		}
		var jsonStr = JSON.stringify(json);
		$.ajax({
			type:"get",
			url:baseUrl+"customer/addBuyerReceiptAddress",
			data:{"jsonStr":jsonStr},
			async:false,
			success:function(data){
				var result = JSON.parse(data).data;
				if(result=='true'){
					if(isDefault == "1"){
						$(".select_address").find(".block").each(function(){
							$(this).find(".iconfont").remove();
						});
						$(".select_address").append('<div class="block" name="userReceiptAddress">'
								+'<input type="hidden" id="userReceiptAddressMsg"'
								+' data-oneAddressId = '+ provinceId
								+' data-oneAddressName = '+ provinceName
								+' data-twoAddressId = ' + cityId
								+' data-twoAddressName = ' + newCityName
								+' data-threeAddressId = ' + countyId 
								+' data-threeAddressName = ' + newCountyName
								+' data-contactsWay = ' + phone
								+' data-contactsPerson = ' + name
								+' data-detailAddress = '+ detailAddress +'>'
				+'<p>姓名：<span name="userReceiptAddressName">'+name+'</span></p>'
				+'<p>电话：<span name="userReceiptAddressPhone">'+phone+'</span></p>'
				+'<p>地址：<span name="userReceiptAddressDetail">'+provinceName+newCityName+newCountyName+detailAddress+'</span></p>'
				+'<i class="iconfont default">&#xe644;</i>'
				+'</div>');
						$(".address_detail").empty();
						$(".address_detail").append('<p>姓名：<span id="chooseReceiptAddressName">'+name+'</span></p>'
								+'<p>电话：<span id="chooseReceiptAddressPhone">'+phone+'</span></p>'
								+'<input type="hidden" id="chooseReceiptAddressMsg"'
						+' data-oneAddressId = '+ provinceId
						+' data-oneAddressName = '+ provinceName
						+' data-twoAddressId = ' + cityId
						+' data-twoAddressName = ' + newCityName
						+' data-threeAddressId = ' + countyId 
						+' data-threeAddressName = ' + newCountyName
						+' data-contactsWay = ' + phone
						+' data-contactsPerson = ' + name
						+' data-detailAddress = '+ detailAddress+'>'
								+'<p>地址：<span id="chooseReceiptAddressDetail">'+provinceName+newCityName+newCountyName+detailAddress+'</span></p>');
					}else{
						if($("#chooseReceiptAddressName")[0]==undefined){
							$(".select_address").append('<div class="block" name="userReceiptAddress">'
									+'<input type="hidden" id="userReceiptAddressMsg"'
									+' data-oneAddressId = '+ provinceId
									+' data-oneAddressName = '+ provinceName
									+' data-twoAddressId = ' + cityId
									+' data-twoAddressName = ' + newCityName
									+' data-threeAddressId = ' + countyId 
									+' data-threeAddressName = ' + newCountyName
									+' data-contactsWay = ' + phone
									+' data-contactsPerson = ' + name
									+' data-detailAddress = '+ detailAddress+'>'
									+'<p>姓名：<span name="userReceiptAddressName">'+name+'</span></p>'
									+'<p>电话：<span name="userReceiptAddressPhone">'+phone+'</span></p>'
									+'<i class="iconfont default">&#xe644;</i>'
									+'<p>地址：<span name="userReceiptAddressDetail">'+provinceName+newCityName+newCountyName+detailAddress+'</span></p>');
							
							$(".address_detail").empty();
							$(".address_detail").append('<p>姓名：<span id="chooseReceiptAddressName">'+name+'</span></p>'
									+'<p>电话：<span id="chooseReceiptAddressPhone">'+phone+'</span></p>'
									+'<input type="hidden" id="chooseReceiptAddressMsg"'
									+' data-oneAddressId = '+ provinceId
									+' data-oneAddressName = '+ provinceName
									+' data-twoAddressId = ' + cityId
									+' data-twoAddressName = ' + newCityName
									+' data-threeAddressId = ' + countyId 
									+' data-threeAddressName = ' + newCountyName
									+' data-contactsWay = ' + phone
									+' data-contactsPerson = ' + name
									+' data-detailAddress = '+ detailAddress+'>'
									+'<p>地址：<span id="chooseReceiptAddressDetail">'+provinceName+newCityName+newCountyName+detailAddress+'</span></p>');
						}else{
							$(".select_address").append('<div class="block" name="userReceiptAddress">'
									+'<input type="hidden" id="userReceiptAddressMsg"'
									+' data-oneAddressId = '+ provinceId
									+' data-oneAddressName = '+ provinceName
									+' data-twoAddressId = ' + cityId
									+' data-twoAddressName = ' + newCityName
									+' data-threeAddressId = ' + countyId 
									+' data-threeAddressName = ' + newCountyName
									+' data-contactsWay = ' + phone
									+' data-contactsPerson = ' + name
									+' data-detailAddress = '+ detailAddress+'>'
									+'<p>姓名：<span name="userReceiptAddressName">'+name+'</span></p>'
									+'<p>电话：<span name="userReceiptAddressPhone">'+phone+'</span></p>'
									+'<p>地址：<span name="userReceiptAddressDetail">'+provinceName+newCityName+newCountyName+detailAddress+'</span></p>');
						}
						
					}
					$(".add_address").css({
						opacity:"0",
						visibility:"hidden"
					})
					$(".select_address").css({
							opacity:"1",
							visibility:"visible"
					})
					tip.content("添加收货地址成功").show();
				}else{
					tip.content("添加收货地址失败").show();
				}
			},
			error:function(){
			}
		});
		
	}else{
		tip.content("必填项不能为空!").show();
	}
});
//设为默认地址按钮
$("#beDefault").on("click",function(){
	$(this).toggleClass("iconfont").toggleClass("icon-icon");
});
//用户选择收货地址
$(".select_address").on("click",'div[name="userReceiptAddress"]',function(){
	var chooseName = $(this).find('span[name="userReceiptAddressName"]').text();
	var choosePhone = $(this).find('span[name="userReceiptAddressPhone"]').text();
	var chooseDetail = $(this).find('span[name="userReceiptAddressDetail"]').text();
	var userReceiptAddressMsg = $(this).find("#userReceiptAddressMsg");
	if($("#chooseReceiptAddressName")[0]==undefined){
		$(".address_detail").empty();
		$(".address_detail").append('<p>姓名：<span id="chooseReceiptAddressName">'+chooseName+'</span></p>'
				+'<p>电话：<span id="chooseReceiptAddressPhone">'+choosePhone+'</span></p>'
				+'<input type="hidden" id="chooseReceiptAddressMsg"'
				+' data-oneAddressId = '+ userReceiptAddressMsg.attr("data-oneAddressId")
				+' data-oneAddressName = '+ userReceiptAddressMsg.attr("data-oneAddressName")
				+' data-twoAddressId = ' + userReceiptAddressMsg.attr("data-twoAddressId")
				+' data-twoAddressName = ' + userReceiptAddressMsg.attr("data-twoAddressName")
				+' data-threeAddressId = ' + userReceiptAddressMsg.attr("data-threeAddressId")
				+' data-threeAddressName = ' + userReceiptAddressMsg.attr("data-threeAddressName")
				+' data-contactsWay = ' + userReceiptAddressMsg.attr("data-contactsWay")
				+' data-contactsPerson = ' + userReceiptAddressMsg.attr("data-contactsPerson")
				+' data-detailAddress = '+ userReceiptAddressMsg.attr("data-detailAddress")+'>'
				+'<p>地址：<span id="chooseReceiptAddressDetail">'+chooseDetail+'</span></p>');
	}else{
		$("#chooseReceiptAddressName").text(chooseName);
		$("#chooseReceiptAddressPhone").text(choosePhone);
		$("#chooseReceiptAddressDetail").text(chooseDetail);
		$("#chooseReceiptAddressMsg").attr("data-oneAddressId",userReceiptAddressMsg.attr("data-oneAddressId"));
		$("#chooseReceiptAddressMsg").attr("data-oneAddressName",userReceiptAddressMsg.attr("data-oneAddressName"));
		$("#chooseReceiptAddressMsg").attr("data-twoAddressId",userReceiptAddressMsg.attr("data-twoAddressId"));
		$("#chooseReceiptAddressMsg").attr("data-twoAddressName",userReceiptAddressMsg.attr("data-twoAddressName"));
		$("#chooseReceiptAddressMsg").attr("data-threeAddressId",userReceiptAddressMsg.attr("data-threeAddressId"));
		$("#chooseReceiptAddressMsg").attr("data-threeAddressName",userReceiptAddressMsg.attr("data-threeAddressName"));
		$("#chooseReceiptAddressMsg").attr("data-contactsWay",userReceiptAddressMsg.attr("data-contactsWay"));
		$("#chooseReceiptAddressMsg").attr("data-contactsPerson",userReceiptAddressMsg.attr("data-contactsPerson"));
		$("#chooseReceiptAddressMsg").attr("data-detailAddress",userReceiptAddressMsg.attr("data-detailAddress"));
		
	}
	
	$(".shadow").remove();
	$(".select_address").css({
		opacity:"0",
		visibility:"hidden"
	})
	clickShadow(function(){
		$(".add_address").css({
			opacity:"0",
			visibility:"hidden"
		})
	});
});
//购买认证商品
$("#renzhengBuy").click(function(){
	var chooseReceiptAddress = $("#chooseReceiptAddressMsg");
	if($("#chooseReceiptAddressName")[0]==undefined){
		tip.content("请选择一个地址").show();
	}else if($("#dianming").text()==""){
		tip.content("认证信息不能为空").show();
	}else{
		var json = {
				"oneAddressId": chooseReceiptAddress.attr("data-oneAddressId"),
				"oneAddressName":chooseReceiptAddress.attr("data-oneAddressName"),
				"twoAddressId":chooseReceiptAddress.attr("data-twoAddressId"),
				"twoAddressName":chooseReceiptAddress.attr("data-twoAddressName"),
				"threeAddressId":chooseReceiptAddress.attr("data-threeAddressId"),
				"threeAddressName":chooseReceiptAddress.attr("data-threeAddressName"),
				"contactsWay":chooseReceiptAddress.attr("data-contactsWay"),
				"contactsPerson":chooseReceiptAddress.attr("data-contactsPerson"),
				"detailAddress":chooseReceiptAddress.attr("data-detailAddress"),
		}
		var jsonStr = JSON.stringify(json);
		post(baseUrl+"pay/payPurchaseCertification",{"jsonStr":jsonStr});
	}
});

})
window.onscroll = function(){
	lazyLoad($(".lazy[data-load='false']"));
}